package sysAuth

import sysUtil.ResultCode
import sysUtil.ResultJson
import sysUtil.SysCommUtil
import xdInterface.XdControllerInterface

class OrganizationController implements XdControllerInterface {


    def authUtilsService

    def index() {query()}

    @Override
    def delete() {
        del()
    }


    /**
     * 更新区域信息
     * @return
     */
    def update(){
        def result = ResultJson.resultJson()

        try {
            def id = params.id
            SysOrganization sysOrganization = SysOrganization.get(id)

            sysOrganization.setName(params.name?:sysOrganization.name)
            sysOrganization.setOrgNo(params.orgNo?:sysOrganization.orgNo)
            sysOrganization.setState(params.state?:sysOrganization.state)
            sysOrganization.setOrderNum(params.orderNum ? Integer.parseInt(params.orderNum):sysOrganization.orderNum)
            sysOrganization.setParentId(params.parentId?Long.parseLong(params.parentId):sysOrganization.parentId)
            sysOrganization.setUpdater(params.updater)
            sysOrganization.setMarks(params.marks?:sysOrganization.marks)

            sysOrganization.save(flush: true)
            result.put(ResultJson.CODE.name, ResultCode.TABLE_DATA_SUCCESS.code)
            result.put(ResultJson.MSG.name, ResultCode.OPT_SUCCESS.message)


        } catch (Exception e) {
            result.put(ResultJson.CODE.name, ResultCode.QUERY_ERROR.code)
            result.put(ResultJson.MSG.name, ResultCode.QUERY_ERROR.message)
            e.printStackTrace()
        } finally {
            //println result;
            render result.toString()
        }
    }

    /**
     * 心中节点
     * @return
     */
    def add(){
        def result = ResultJson.resultJson()

        try {
            def parentId = params.parentId?Long.parseLong(params.parentId):null
            def name = params.name?:"New Node"
            def orgNo = params.orgNo?:UUID.randomUUID()
            SysOrganization sysOrganization =  new SysOrganization(orgNo:orgNo,name:name,
                                                parentId:parentId,creator: params.userNo,orderNum: 0)
            sysOrganization.save(flush: true)
            result.put(ResultJson.CODE.name, ResultCode.TABLE_DATA_SUCCESS.code)
            result.put(ResultJson.MSG.name, ResultCode.OPT_SUCCESS.message)
            result.put(ResultJson.DATA.name,SysCommUtil.domainToMap(sysOrganization))
        } catch (Exception e) {
            result.put(ResultJson.CODE.name, ResultCode.QUERY_ERROR.code)
            result.put(ResultJson.MSG.name, ResultCode.QUERY_ERROR.message)
            e.printStackTrace()
        } finally {
            //println result;
            render result.toString()
        }
    }

    @Override
    def dowLoad() {
        return null
    }

    @Override
    def upLoad() {
        return null
    }

    @Override
    def query() {
        listOrgTreeData()
    }

    @Override
    def getOne() {
        return null
    }

    @Override
    def detail() {
        return null
    }

    def del(){
        def result = ResultJson.resultJson()
        try {
            def id = params.id
            SysOrganization sysOrganization =  SysOrganization.get(id)
            def children = SysOrganization.findByParentId(sysOrganization.id)
            //println SysOrganization.count
            if(SysOrganization.count == 1 || sysOrganization.creator != params.userNo){
                result.put(ResultJson.CODE.name, ResultCode.OPT_NOT_AUTH.code)
                result.put(ResultJson.MSG.name, ResultCode.OPT_NOT_AUTH.message)
            }else if(children){
                result.put(ResultJson.CODE.name, ResultCode.EXISTS_CHILD_ERROR.code)
                result.put(ResultJson.MSG.name, ResultCode.EXISTS_CHILD_ERROR.message)
            }else {
                sysOrganization.delete(flush: true)
                result.put(ResultJson.CODE.name, ResultCode.TABLE_DATA_SUCCESS.code)
                result.put(ResultJson.MSG.name, ResultCode.OPT_SUCCESS.message)
            }
        } catch (Exception e) {
            result.put(ResultJson.CODE.name, ResultCode.QUERY_ERROR.code)
            result.put(ResultJson.MSG.name, ResultCode.QUERY_ERROR.message)
            e.printStackTrace()
        } finally {
            //println result;
            render result.toString()
        }
    }

    @Override
    def edit() {
        return null
    }

    @Override
    def save() {
        return null
    }

    /**
     * 根据角色ID，获取数据权限，并设置选中状态
     */
    def listOrgTreeData(){
        def result = ResultJson.resultJson()
        result.put(ResultJson.DATA.name, authUtilsService.listOrgTreeData(params))
        render result.toString()
    }


}
